package im.starchat.chat.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.adapter.MsgAdapter;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.MsgBean;
import im.starchat.chat.view.swipeRefreshLayout.SwipeRefreshLayout;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class MainTabMsgFragment extends Fragment implements OnClickListener , SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    private MsgAdapter mMsgAdapter;

    private LinearLayoutManager mLayoutManager;

    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_refresh_layout_list)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;
    public static MainTabMsgFragment newInstance() {
        return new MainTabMsgFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg, container, false);
        ButterKnife.inject(this, view);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mMsgAdapter = new MsgAdapter(getActivity());
        mRecyclerView.setAdapter(mMsgAdapter);

        getMsgList();
        mSwipeRefreshLayout.setRefreshing(true);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    private void getMsgList(){
        XLApplication.getInstance().getRestClient().getNotices(mCurrentPage, RestClient.DEFAULT_PREPAGE, new Callback<MsgBean[]>() {
            @Override
            public void success(MsgBean[] msgBean, Response response) {
                if (mCurrentPage == RestClient.DEFAULT_PAGE) {
                    mMsgAdapter.refreshData(Arrays.asList(msgBean));
                } else {
                    mMsgAdapter.addAll(Arrays.asList(msgBean));
                }
                if (msgBean.length < 20) {
                    mHasMoreData = false;
                }else{
                    mHasMoreData = true;
                }
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }

            @Override
            public void failure(RetrofitError error) {
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }
        });
    }

    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getMsgList();
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            getMsgList();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            Toast.makeText(getActivity(), "没有更多数据", Toast.LENGTH_SHORT).show();
        }
    }


}
